package net.crashtoot.picasa.web.rest;

import java.util.ArrayList;
import java.util.List;

import net.crashtoot.picasa.core.model.Account;
import net.crashtoot.picasa.core.model.Album;
import net.crashtoot.picasa.core.service.AccountService;
import net.crashtoot.picasa.core.service.PhotoService;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/album")
public class AlbumController {
    private static final Log log = LogFactory.getLog(AlbumController.class);

    @Autowired
    private AccountService accountService;

    @Autowired
    private PhotoService photoService;

    /**
     * @return tous les albums de tous les utilisateurs.
     * @throws Exception
     */
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public List<Album> listAlbums() throws Exception {
        List<Account> accounts = accountService.listAll();

        if (accounts == null || accounts.size() == 0) {
            return null;
        }

        List<Album> albums = new ArrayList<Album>();
        for (Account account : accounts) {
            albums.addAll(photoService.getAlbums(account));
        }
        log.debug(albums.size() + " album(s) pour " + accounts.size() + " utilisateur(s).");

        return albums;
    }

    /**
     * @param userId : username
     * @return les albums de l'utilisateur
     * @throws Exception
     */
    @RequestMapping(value = "/{userId:.*}/")
    public List<Album> listAllAlbums(@PathVariable String userId) throws Exception {
        Account account = accountService.getAccountByUserName(userId);
        if (account == null) {
            return null;
        }
        List<Album> albums = photoService.getAlbums(account);

        return albums;
    }
}
